package app.displayTree.fixer.shape {
    import copyengine.infoTree.meta.CESAnimationEachFrameInfoMeta;

    import flash.display.Sprite;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;

    public final class DisplayTreeShapeFixer {
        public function DisplayTreeShapeFixer() {
        }

        /**
         * 修复所有ShapeMeta的错误信息
         */
        public function fixShapeMetaProperty(_allShapeMetaWarpVector:Vector.<ShapeMetaWarp>):void {
            var length:int = _allShapeMetaWarpVector.length;
            for (var i:int = 0; i < length; i++) {
                doFixShapeMeta(_allShapeMetaWarpVector[i]);
            }
        }

        private function doFixShapeMeta(_warp:ShapeMetaWarp):void {
            var oringalMatrix:Matrix;
            oringalMatrix = _warp.sourceTarget.transform.matrix;
            _warp.sourceTarget.transform.matrix = new Matrix();
            var warpSp:Sprite = new Sprite();
            warpSp.addChild(_warp.sourceTarget);
            var re:Rectangle = warpSp.getBounds(warpSp);
            _warp.sourceTarget.transform.matrix = oringalMatrix;

            var length:int = _warp.shapeMeta.frameTransformMatrixArray.length;
            for (var i:int = 0; i < length; i++) {
                var framInfo:CESAnimationEachFrameInfoMeta = _warp.shapeMeta.frameTransformMatrixArray[i];
                var matrix:Matrix = framInfo.getMatrix();
                //Fixer首先要把Matrix中的tx和ty调整为0,因为之前
                //生成的ShapMeta中不仅x,y有Bug,Matrix中的tx,ty也受到影响
                //				matrix.tx=matrix.ty=0;

                var newMatrxi:Matrix = new Matrix();
                newMatrxi.translate(re.x, re.y);
                newMatrxi.concat(matrix);
                //Shape需要使用pivotX和pivotY作为x,y属性(看有道Note)
                framInfo.setMatrix(newMatrxi);
            }
        }


    }
}
